%pure_parser

%token _BANG_t
%token _BANG_EQUALS_t
%token _AMPER_t
%token _AMPERAMPER_t
%token _LPAREN_t
%token _RPAREN_t
%token _PLUS_EQUALS_t
%token _COLON_t
%token _SEMIC_t
%token _LANGLE_t
%token _LANGLE_EQUALS_t
%token _EQUALS_t
%token _RANGLE_t
%token _RANGLE_EQUALS_t
%token _QUESTION_EQUALS_t
%token _LBRACKET_t
%token _RBRACKET_t
%token ACTIONS_t
%token BIND_t
%token CASE_t
%token CLASS_t
%token DEFAULT_t
%token ELSE_t
%token EXISTING_t
%token FOR_t
%token IF_t
%token IGNORE_t
%token IN_t
%token INCLUDE_t
%token LOCAL_t
%token MODULE_t
%token ON_t
%token PIECEMEAL_t
%token QUIETLY_t
%token RETURN_t
%token RULE_t
%token SWITCH_t
%token TOGETHER_t
%token UPDATED_t
%token WHILE_t
%token _LBRACE_t
%token _BAR_t
%token _BARBAR_t
%token _RBRACE_t

%token ARG STRING

%left _BARBAR_t _BAR_t
%left _AMPERAMPER_t _AMPER_t
%left _EQUALS_t _BANG_EQUALS_t IN_t
%left _LANGLE_t _LANGLE_EQUALS_t _RANGLE_t _RANGLE_EQUALS_t
%left _BANG_t

%{
#include "precompiled.hpp"
#include "parse.hpp"
#include "scan.hpp"
#include "jam_ast.hpp"

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4065)
#endif

#include <malloc.h>
#define YYMALLOC malloc
#define YYFREE free

# define YYMAXDEPTH 10000	/* for OSF and other less endowed yaccs */

//# define F0 (LIST *(*)(PARSE *, FRAME *))0
//# define P0 (PARSE *)0
//# define S0 (char *)0

# define pappend( l,r )		make_node(context, new append_node(l,r),l,r)
# define peval( c,l,r )		make_node(context, new eval_node(l,r,c),l)
# define pfor( s,l,r,x,sts )	make_node(context, new foreach_node(l,r,s,x),sts)
# define pif( l,r,t,sts )	  	make_node(context, new if_node(l,r,t),sts)
# define pincl( l,sts )       	make_node(context, new include_node(l),sts)
# define plist( s,sts )	  		make_node(context, new list_node(s),sts)
# define plocal( l,r,t,sts )  	make_node(context, new local_node(l,r,t),sts)
# define pmodule( l,r,sts )		make_node(context, new module_node(l,r),sts)
# define pclass( l,r,sts )		make_node(context, new class_node(l,r),sts)
# define pnull()	  		make_null_node(context, new null_node)
# define pon( l,r )	  		make_node(context, new on_node(l,r),l)
# define prule( s,p,sts )	make_node(context, new rule_node(p,s),sts )
# define prules( l,r )		make_node(context, new rules_node(l,r),l)

# define pset( l,r,a )		make_node(context, new set_node(l,r,a),l)
# define pset1( l,r,t,a )	make_node(context, new settings_node(l,r,t,a),l)
# define psetc( s,p,a,l,sts)	make_node(context, new setcomp_node(p,a,s,l),sts)
# define psete( s,l,s1,f,sts )	make_node(context, new setexec_node(l,s,s1,f),sts)
# define pswitch( l,r,sts )		make_node(context, new switch_node(l,r),sts)
# define pwhile( l,r,sts )   	make_node(context, new while_node(l,r),sts)

# define no_node parse_node_ptr()
# define pnode( l,r )    	make_node(context, new chain_node(l,r),l)
# define case_node( s,l,sts )   make_node(context, new case_node(l,s),sts)
# define lolnode( chain,elem )   make_node(context, new lol_node(elem,chain),elem)
# define pret(l,sts)		make_node(context, new return_node(l), sts)

# define prbr_node(sts) make_node(context, new right_brace_node(),sts)
# define plbr_node(b,r,sts) make_node(context, new left_brace_node(b,r),sts)

%}

%%

run	: /* empty */
		/* do nothing */
	| rules
		{ parse_save(context, $1.parse ); }
	;

/*
 * block - zero or more rules
 * rules - one or more rules
 * rule - any one of jam's rules
 * right-recursive so rules execute in order.
 */

block	: null
                { $$.parse = $1.parse; }
	| rules
		{ $$.parse = $1.parse; }
	;

rules	: rule
		{ $$.parse = $1.parse; }
	| rule rules
		{ $$.parse = prules( $1.parse, $2.parse ); }
	| LOCAL_t list assign_list_opt _SEMIC_t block
		{ $$.parse = plocal( $2.parse, $3.parse, $5.parse,$1 ); }
	;

null    : /* empty */
        { $$.parse = pnull(); }
        ;

assign_list_opt : _EQUALS_t list
                { $$.parse = $2.parse; $$.number = ASSIGN_SET; }
        | null
		{ $$.parse = $1.parse; $$.number = ASSIGN_APPEND; }
        ;

arglist_opt : _LPAREN_t lol _RPAREN_t
                { $$.parse = $2.parse; }
        |
                { $$.parse = no_node; }
        ;

local_opt : LOCAL_t
                { $$.number = 1; }
          | /* empty */
                { $$.number = 0; }
          ;
          
braced_block : _LBRACE_t block _RBRACE_t
		{ $$.parse = plbr_node($2.parse,prbr_node($3),$1); }
		;

braced_case_block : _LBRACE_t cases _RBRACE_t
		{ $$.parse = plbr_node($2.parse,prbr_node($3),$1); }
		;

rule	: braced_block
	| INCLUDE_t list _SEMIC_t
		{ $$.parse = pincl( $2.parse, $1 ); }
	| ARG lol _SEMIC_t
		{ $$.parse = prule( $1.string, $2.parse, $1 ); }
	| arg assign list _SEMIC_t
		{ $$.parse = pset( $1.parse, $3.parse, to_assignment($2.number) ); }
	| arg ON_t list assign list _SEMIC_t
		{ $$.parse = pset1( $1.parse, $3.parse, $5.parse, to_assignment($4.number) ); }
	| RETURN_t list _SEMIC_t
		{ $$.parse = pret($2.parse, $1); } //$2.parse; }
	| FOR_t local_opt ARG IN_t list braced_block
		{ $$.parse = pfor( $3.string, $5.parse, $6.parse, !!$2.number, $1 ); }
	| SWITCH_t list braced_case_block
		{ $$.parse = pswitch( $2.parse, $3.parse, $1 ); }
	| IF_t expr braced_block
		{ $$.parse = pif( $2.parse, $3.parse, pnull(),$1 ); }
	| MODULE_t list braced_block
		{ $$.parse = pmodule( $2.parse, $3.parse, $1 ); }
	| CLASS_t lol braced_block
		{ $$.parse = pclass( $2.parse, $3.parse, $1 ); }
	| WHILE_t expr braced_block
		{ $$.parse = pwhile( $2.parse, $3.parse, $1 ); }
	| IF_t expr braced_block ELSE_t rule
		{ $$.parse = pif( $2.parse, $3.parse, $5.parse,$1 ); }
     | local_opt RULE_t ARG arglist_opt rule
		{ $$.parse = psetc( $3.string, $5.parse, $4.parse, !!$1.number, $2 ); }
	| ON_t arg rule
		{ $$.parse = pon( $2.parse, $3.parse ); }
	| ACTIONS_t eflags ARG bindlist _LBRACE_t
		{ GET_SCANNER.set_scan_mode( SCAN_STRING ); }
	  STRING 
		{ GET_SCANNER.set_scan_mode( SCAN_NORMAL ); }
	  _RBRACE_t
		{ $$.parse = psete( $3.string,$4.parse,$7.string,$2.number,$1 ); }
	;

/*
 * assign - = or +=
 */

assign	: _EQUALS_t
		{ $$.number = ASSIGN_SET; }
	| _PLUS_EQUALS_t
		{ $$.number = ASSIGN_APPEND; }
	| _QUESTION_EQUALS_t
		{ $$.number = ASSIGN_DEFAULT; }
	| DEFAULT_t _EQUALS_t
		{ $$.number = ASSIGN_DEFAULT; }
	;


/*
 * expr - an expression for if
 */
expr	: arg 
		{ $$.parse = peval( EXPR_EXISTS, $1.parse, pnull() ); }
	| expr _EQUALS_t expr 
		{ $$.parse = peval( EXPR_EQUALS, $1.parse, $3.parse ); }
	| expr _BANG_EQUALS_t expr
		{ $$.parse = peval( EXPR_NOTEQ, $1.parse, $3.parse ); }
	| expr _LANGLE_t expr
		{ $$.parse = peval( EXPR_LESS, $1.parse, $3.parse ); }
	| expr _LANGLE_EQUALS_t expr 
		{ $$.parse = peval( EXPR_LESSEQ, $1.parse, $3.parse ); }
	| expr _RANGLE_t expr 
		{ $$.parse = peval( EXPR_MORE, $1.parse, $3.parse ); }
	| expr _RANGLE_EQUALS_t expr 
		{ $$.parse = peval( EXPR_MOREEQ, $1.parse, $3.parse ); }
	| expr _AMPER_t expr 
		{ $$.parse = peval( EXPR_AND, $1.parse, $3.parse ); }
	| expr _AMPERAMPER_t expr 
		{ $$.parse = peval( EXPR_AND, $1.parse, $3.parse ); }
	| expr _BAR_t expr
		{ $$.parse = peval( EXPR_OR, $1.parse, $3.parse ); }
	| expr _BARBAR_t expr
		{ $$.parse = peval( EXPR_OR, $1.parse, $3.parse ); }
	| arg IN_t list
		{ $$.parse = peval( EXPR_IN, $1.parse, $3.parse ); }
	| _BANG_t expr
		{ $$.parse = peval( EXPR_NOT, $2.parse, pnull() ); }
	| _LPAREN_t expr _RPAREN_t
		{ $$.parse = $2.parse; }
	;


/*
 * cases - action elements inside a 'switch'
 * case - a single action element inside a 'switch'
 * right-recursive rule so cases can be examined in order.
 */

cases	: /* empty */
		{ $$.parse = no_node; } /*TODO: is this OK?*/
	| case cases
		{ $$.parse = pnode( $1.parse, $2.parse ); }
	;

case	: CASE_t ARG _COLON_t block
		{ $$.parse = case_node( $2.string, $4.parse, $1 ); }
	;

/*
 * lol - list of lists
 * right-recursive rule so that lists can be added in order.
 */

lol	: list
		{ $$.parse = lolnode( no_node, $1.parse ); } /*TODO: is this OK?*/
	| list _COLON_t lol
		{ $$.parse = lolnode( $3.parse, $1.parse ); }
	;

/*
 * list - zero or more args in a LIST
 * listp - list (in puncutation only mode)
 * arg - one ARG or function call
 */

list	: listp
		{ $$.parse = $1.parse; GET_SCANNER.set_scan_mode( SCAN_NORMAL ); }
	;

listp	: /* empty */
		{ $$.parse = pnull(); GET_SCANNER.set_scan_mode( SCAN_PUNCT ); }
	| listp arg
        	{ $$.parse = pappend( $1.parse, $2.parse ); }
	;

arg	: ARG 
		{ $$.parse = plist( $1.string, $1 ); }
	| _LBRACKET_t { GET_SCANNER.set_scan_mode( SCAN_NORMAL ); } func _RBRACKET_t
		{ $$.parse = $3.parse; }
	;

/*
 * func - a function call (inside [])
 * This needs to be split cleanly out of 'rule'
 */

func	: arg lol
		{ $$.parse = prule( $1.string, $2.parse, $1 ); }
	| ON_t arg arg lol
		{ $$.parse = pon( $2.parse, prule( $3.string, $4.parse, $3 ) ); }
	| ON_t arg RETURN_t list 
		{ $$.parse = pon( $2.parse, $4.parse ); }
	;


/*
 * eflags - zero or more modifiers to 'executes'
 * eflag - a single modifier to 'executes'
 */

eflags	: /* empty */
		{ $$.number = 0; }
	| eflags eflag
		{ $$.number = $1.number | $2.number; }
	;

eflag	: UPDATED_t
		{ $$.number = EXEC_UPDATED; }
	| TOGETHER_t
		{ $$.number = EXEC_TOGETHER; }
	| IGNORE_t
		{ $$.number = EXEC_IGNORE; }
	| QUIETLY_t
		{ $$.number = EXEC_QUIETLY; }
	| PIECEMEAL_t
		{ $$.number = EXEC_PIECEMEAL; }
	| EXISTING_t
		{ $$.number = EXEC_EXISTING; }
	;


/*
 * bindlist - list of variable to bind for an action
 */

bindlist : /* empty */
		{ $$.parse = pnull(); }
	| BIND_t list
		{ $$.parse = $2.parse; }
	;
